package com.hjq.fcloud.utils

import com.hjq.fcloud.app.AppActivity
import com.hjq.fcloud.bean.CommonFileBean
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers

/**
 * 公共文件上传
 */
object CommonUploadUtils {

    private val mZipImageHelper: ZipImageUtils by lazy { ZipImageUtils() }

    fun uploadFile(
        appActivity: AppActivity,
        filePath: String,
        callBack: (success: Boolean, errorMsg: String?, urls: List<CommonFileBean>?) -> Unit,
        zipImages: Boolean? = true
    ) {
        var filePaths = mutableListOf<CommonFileBean>()
        filePaths.add(CommonFileBean(filePath))
        uploadFiles(appActivity, filePaths, callBack, zipImages)
    }

    fun uploadFiles(
        appActivity: AppActivity,
        filePaths: List<CommonFileBean>,
        callBack: (success: Boolean, errorMsg: String?, urls: List<CommonFileBean>?) -> Unit,
        zipImages: Boolean? = true
    ) {
        //如果已经是网络文件了则不需要再上传
        var networkPaths = filePaths.filter { it.url.startsWith("http") }
        var filePaths = filePaths.filter { !it.url.startsWith("http") }
        if (filePaths == null || filePaths.size == 0) {
            callBack(true, null, networkPaths)
            return
        }

        perfromUploadFile(appActivity, zipImages, filePaths
        ) { success, errorMsg, urls->
            var fullUrls = ArrayList<CommonFileBean>()
            if(networkPaths != null && networkPaths.size > 0){
                fullUrls.addAll(networkPaths)
            }
            if (urls != null) {
                fullUrls.addAll(urls)
            }
            callBack(success, errorMsg, fullUrls)
        }
    }

    private fun perfromUploadFile(
        appActivity: AppActivity,
        zipImages: Boolean?,
        filePaths: List<CommonFileBean>,
        callBack: (success: Boolean, errorMsg: String?, urls: List<CommonFileBean>?) -> Unit
    ) {
        appActivity.showDialog()
        if (zipImages == true) {
            //上传前先压缩图片
            mZipImageHelper.zipImages(
                appActivity.applicationContext,
                filePaths
            ) { zipSuccess, errorMsg: String?, filePaths ->
                if (zipSuccess) {
                    rxUploadFile(appActivity, filePaths!!, callBack)
                } else {
                    callBack(false, errorMsg, null)
                    appActivity.hideDialog()
                }
            }
        } else {
            rxUploadFile(appActivity, filePaths, callBack)
        }
    }

    private fun rxUploadFile(
        appActivity: AppActivity,
        fileItems: List<CommonFileBean>,
        callBack: (success: Boolean, errorMsg: String?, urls: List<CommonFileBean>?) -> Unit
    ) {
        var retList = mutableListOf<CommonFileBean>()
        fileItems.forEach { fileItem ->
            Observable.create<CommonFileBean> {
                var ret = HuaweiObsUtils.uploadFile(fileItem)
                it.onNext(ret)
            }.subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe { ret ->
                    retList.add(ret)
                    //全部上传完成
                    var uploadFinish = (fileItems.size == retList.size)
                    if (uploadFinish) {
                        appActivity.hideDialog()
                        //是否有失败的情况
                        var hasFail = fileItems.filter { !it.uploasSuccess }.size > 0
                        if (hasFail) {
                            //返回失败
                            callBack(false, "文件上传失败", null)
                        } else {
                            callBack(true, null, fileItems)
                        }
                    }
                }
        }
    }

}